package com.vito.base.authoriza.action;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.authoriza.service.SectionService;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.tools.I18nResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.section.model.SectionModel;

@Controller
@RequestMapping("/dcs/section")
public class SectionAction extends BaseAction{

	private static final Logger log = LoggerFactory.getLogger(SectionAction.class);
	
	@Reference
	private SectionService secService;
	
	@RequestMapping("/querySectionByPage")
	@ResponseBody
	public Result querySectionByPage(SectionModel sectionModel){
		Result res = new Result();
		try {
			DataGrid<SectionModel> data = secService.querySectionByPage(sectionModel);
			res.setCode(Result.CODE_OK);
			res.setData(data);
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.EXECERR());
		}
		return res;
	}
	@RequestMapping("/queryWorkArea")
	@ResponseBody
	public Result queryWorkArea(SectionModel sectionModel){
		Result res = new Result();
		try {
			List<SectionModel> queryWorkArea = secService.queryWorkArea(sectionModel);
			res.setCode(Result.CODE_OK);
			res.setData(queryWorkArea);
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
			res.setMsg(I18nResMsg.EXECERR());
		}
		return res;
	}
	@RequestMapping("/deleteImpData")
	@ResponseBody
	public Result deleteImpData(@RequestParam Map<String,Object> map){
		Result res = new Result();
		try {
			secService.deleteImpData(map);
			res.setCode(Result.CODE_OK);
		} catch (Exception e) {
			res.setCode(Result.CODE_ERROR);
		}
		return res;
	}
	
	@RequestMapping("/queryRoadFromImportByPage")
	@ResponseBody
	public Result queryMemberFromImportByPage(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			DataGrid<MyHashMap<String, Object>> data = secService.queryMemberFromImportByPage(map);
			result.setCode(Result.CODE_OK);
			result.setData(data);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	@RequestMapping("saveRoadValidate")
	@ResponseBody
	public Result saveMemberValidate(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			if(map == null || map.isEmpty() || Validata.isNullOrEmpty(map.get("batchId").toString())){
				result.setCode(Result.CODE_PARA_ERROR);
				result.setMsg("批次号不能为空");
				return result;
			}
			secService.saveValidateNew(map);
			Integer errorCount = secService.queryErrorCount(map);
			map.put("errorCount", errorCount);
			result.setCode(Result.CODE_OK);
			result.setData(map);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	@RequestMapping("queryImpRoadErrorByPage")
	@ResponseBody
	public Result queryImpMemberErrorByPage(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			if(map == null || map.isEmpty() || Validata.isNullOrEmpty(map.get("batchId").toString())){
				result.setCode(Result.CODE_PARA_ERROR);
				result.setMsg("批次号不能为空");
				return result;
			}
			DataGrid<MyHashMap<String, Object>> data = secService.queryImpMemberErrorByPage(map);
			result.setCode(Result.CODE_OK);
			result.setData(data);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	@RequestMapping("saveImpRoad")
	@ResponseBody
	public Result saveImpMember(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			if(map == null || map.isEmpty() || Validata.isNullOrEmpty(map.get("batchId").toString())){
				result.setCode(Result.CODE_PARA_ERROR);
				result.setMsg("批次号不能为空");
				return result;
			}
			
			UserModel obj = SessionMap.getObj();
			if(obj == null || Validata.isNullOrEmpty(obj.getUserId())){
				result.setCode(Result.CODE_LOGIN_REQUIRE);
				result.setMsg(I18nResMsg.MSG_LOGIN_NEED());
				return result;
			}
			map.put("userId", obj.getUserId());
			Map<String, Object> data = secService.saveImp(map);
			result.setCode(Result.CODE_OK);
			result.setData(data);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	
	@RequestMapping("queryProjectTree")
	@ResponseBody
	public Result queryProjectTree(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			List<Map<String, Object>> list = secService.queryProjectTree(map);
			result.setCode(Result.CODE_OK);
			result.setData(list);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	
	@RequestMapping("savePile")
	@ResponseBody
	public Result savePile(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			String savePile = secService.savePile(map);
			result.setCode(Result.CODE_OK);
			result.setData(savePile);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
	@RequestMapping("queryAllGPS")
	@ResponseBody
	public Result queryAllGPS(@RequestParam Map<String,Object> map){
		Result result = new Result();
		try {
			List<Map<String, Object>> queryAllGPS = secService.queryAllGPS(map);
			result.setCode(Result.CODE_OK);
			result.setData(queryAllGPS);
		} catch (Exception e) {
			log.debug(ResMsg.EXECERR, e);
			result.setCode(Result.CODE_ERROR);
			result.setMsg(I18nResMsg.EXECERR());
		}
		return result;
	}
}
